<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login and Register</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        .container {
            max-width: 300px;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        .form-group button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .form-group button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>Register</h2>
        <div class="form-group">
            <label for="register-username">Username:</label>
            <input type="text" id="register-username">
        </div>
        <div class="form-group">
            <label for="register-password">Password:</label>
            <input type="password" id="register-password">
        </div>
        <div class="form-group">
            <button onclick="register()">Register</button>
        </div>

        <h2>Login</h2>
        <div class="form-group">
            <label for="login-username">Username:</label>
            <input type="text" id="login-username">
        </div>
        <div class="form-group">
            <label for="login-password">Password:</label>
            <input type="password" id="login-password">
        </div>
        <div class="form-group">
            <button onclick="login()">Login</button>
        </div>
    </div>

    <script>
        function register() {
            const username = document.getElementById('register-username').value;
            const password = document.getElementById('register-password').value;

            fetch('/register/submit', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, password })
            })
            .then(response => response.json())
            .then(data => {
                alert('Registration successful: ' + JSON.stringify(data));
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }

        function login() {
            const username = document.getElementById('login-username').value;
            const password = document.getElementById('login-password').value;

            fetch('/login/submit', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, password })
            })
            .then(response => response.json())
            .then(data => {
                if (data.status == 'Login successful') {
                    const encodeUsername = encodeURIComponent(username);
                    window.location.href = `http://127.0.0.1:8866/list?username=${encodeUsername}`;
                }
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }
    </script>
</body>
</html>